var express = require('express');
var router = express.Router();
const moment = require('moment');
//产品list
router.get('/product/list', function (req, res, next) {
    res.render('admin_tmpl/product/list.html', {});
});

//产品添加
router.get('/product/add', async (req, res, next) => {
    var parentSQL = 'select * from t_product_category where delete_flag = 0 and parent is null'
    await common.prototype.operation(parentSQL).then(rest => {
        res.render('admin_tmpl/product/add.html', {parent: rest});
    }, (error) => {
        console.log(error)
        res.send(error)
    }).catch((err) => {
        console.log(err)
    })

});

//产品修改
router.get('/product/edit', async (req, res, next) => {
    var id = req.query.id;
    var sonData;
    var parentData;
    var productData;
    var SQL = 'select * from t_product_category where delete_flag = 0 and parent is null';
    var editSQL = 'select * from t_product where delete_flag = 0 and id="' + id + '"';
    await common.prototype.operation(SQL).then(async rest => {
        parentData = rest;
        var belongto = rest[0].title;
        var sonSQL = 'select * from t_product_category where delete_flag = 0 and belongto="' + belongto + '"';
        await common.prototype.operation(sonSQL).then(async result => {
            sonData = result;
        })

        await common.prototype.operation(editSQL).then(async editRest => {
            productData = editRest[0];
        })
        res.render('admin_tmpl/product/edit.html', {product: productData, parent: parentData, son: sonData});
    }, (error) => {
        console.log(error)
    }).catch((err) => {
        console.log(err)
    })

});

/*
 * 产品列表
 * @return
 *
 */
router.post('/product/getList', async (req, res, next) => {
    var condition = '';
    var SQL = '';
    var conditionSQL = '';
    var allData = '';
    var begin = 0;
    var data = req.body;
    var pageSize = data.pageSize;
    var pageNumber = data.pageNumber;
    var likeTitle = data.search_like_title;
    if (pageSize) {
        begin = (pageNumber - 1) * pageSize;
        condition = condition + 'order by id desc limit ' + begin + ',' + pageSize + ''
    }
    if (likeTitle) {
        SQL = 'select * from t_product where delete_flag = 0 and title like "%' + likeTitle + '%"'
    } else {
        SQL = 'select * from t_product where delete_flag = 0'
    }
    await common.prototype.operation(SQL).then(async rest => {
        allData = rest;
        if (likeTitle) {
            conditionSQL = 'select * from t_product where delete_flag = 0 and title like "%' + likeTitle + '%"' + condition + ''
        } else {
            conditionSQL = 'select * from t_product where delete_flag = 0 ' + condition + ''
        }
        await common.prototype.operation(conditionSQL).then(async result => {
            res.send({
                code: 0, msg: '操作成功',
                count: allData.length, data: result
            })
        })

    }, (error) => {
        console.log(error)
        res.send({
            code: 404, msg: '操作失败',
            data: error
        })
    }).catch((err) => {
        console.log(err)
    })
});

/*
 * 通过分类标题查询
 * @return
 *
 */
// router.post('/product/sonCategory', async (req, res, next) => {
//     var parent = req.body.parent;
//     var SQL = 'select * from t_product_category where delete_flag = 0 and parent="' + parent + '"';
//     await common.prototype.operation(SQL).then(rest => {
//         res.send({
//             code: 0, msg: '操作成功',
//             data: rest
//         })
//     }, (error) => {
//         console.log(error)
//         res.send({
//             code: 404, msg: '操作失败',
//             data: error
//         })
//     }).catch((err) => {
//         console.log(err)
//     })
// });


/*
 * 添加产品
 * @return
 *
 */
router.post('/product/addProduct', async (req, res, next) => {
    var data = req.body;
    var imgList = JSON.stringify("'" + data.imgList + "'");
    var date = moment().format('YYYY-MM-DD HH:mm:ss');
    var SQL = 'INSERT INTO `t_product`(pic,title,series_name,img_list,parent_id,create_date,modify_date,delete_flag) VALUES("' + data.pic + '","' + data.title + '","' + data.seriesName + '","' + imgList + '","' + data.parentId + '","' + date + '","' + date + '",0) '
    await common.prototype.operation(SQL).then(rest => {
        res.send({
            type: 'success', content: '添加成功'
        })
    }, (error) => {
        console.log(error)
        res.send({
            type: 'error', content: '添加失败'
        })
    }).catch((err) => {
        console.log(err)
    })
});


/*
 * 删除产品
 * @return
 *
 */
router.post('/product/delete', async (req, res, next) => {
    var data = req.body;
    if (data.id) {
        var date = moment().format('YYYY-MM-DD HH:mm:ss');
        var SQL = 'update t_product set delete_flag=1, modify_date="' + date + '" where id="' + data.id + '"'
        await common.prototype.operation(SQL).then(rest => {
            res.send({
                type: 'success', content: '删除成功'
            })
        }, (error) => {
            console.log(error)
            res.send({
                type: 'error', content: '删除失败'
            })
        }).catch((err) => {
            console.log(err)
        })
    } else {
        res.send({
            type: 'apiexception', content: '接口异常'
        })
    }
});

/*
 * 修改产品
 * @return
 *
 */
router.post('/product/update', async (req, res, next) => {
    var data = req.body;
    if (data.id) {
        var date = moment().format('YYYY-MM-DD HH:mm:ss');
        var imgList = JSON.stringify("'" + data.imgList + "'");
        var SQL = 'update t_product set title="' + data.title + '",img_list="' + imgList+ '",series_name="' + data.seriesName + '",pic="' + data.pic + '",parent_id="' + data.parentId + '",modify_date="' + date + '" where id="' + data.id + '"'
        await common.prototype.operation(SQL).then(rest => {
            res.send({
                type: 'success', content: '添加成功'
            })
        }, (error) => {
            console.log(error)
            res.send({
                type: 'error', content: '添加失败'
            })
        }).catch((err) => {
            console.log(err)
        })
    } else {
        res.send({
            type: 'apiexception', content: '接口异常'
        })
    }
});

module.exports = router;